package org.example;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioChannelOption;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * 8通道振动
 */
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Test2 {

    public static void main(String[] args) throws InterruptedException {
            NioEventLoopGroup worker = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.option(NioChannelOption.TCP_NODELAY, true);
            bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        //和服务器建立连接之后的回调
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            ByteBuf buffer = Unpooled.buffer();
                            buffer.writeByte(0x02);
                            buffer.writeIntLE(1);

                            ByteBuf byteBuf = Unpooled.buffer();
                            byteBuf.writeBytes(new byte[]{0x01,0x10});
                            byteBuf.writeBytes(new byte[]{0x04});
                            byteBuf.writeBytes("DWGZ12345678901".getBytes(StandardCharsets.UTF_8));
                            byteBuf.writeBytes("ICCID123456789009876".getBytes(StandardCharsets.UTF_8));
                            byteBuf.writeBytes("version11".getBytes(StandardCharsets.UTF_8));
                            byteBuf.writeIntLE(1);
                            byteBuf.writeByte(2);
                            byteBuf.writeShortLE(3);
                            byteBuf.writeShortLE(4);
                            byteBuf.writeByte(5);
                            byteBuf.writeByte(6);
                            byteBuf.writeByte(7);
                            byteBuf.writeByte(8);
                            byteBuf.writeByte(9);
                            byteBuf.writeShortLE(0);
                            byteBuf.writeFloat(1);
                            byteBuf.writeByte(2);

                            //长度  96   85+11
                            buffer.writeShort((short) byteBuf.readableBytes());
                            //数据
                            buffer.writeBytes(byteBuf);
                            //CRC
                            byte[] crc = crc16CCITTFalse(ByteBufUtil.getBytes(buffer));
                            buffer.writeBytes(crc);
                            buffer.writeBytes(new byte[]{0x0d,0x0a});

                            System.out.println("写出得数据为："+ByteBufUtil.hexDump(buffer));
                            ctx.writeAndFlush(buffer);
                        }
                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            ByteBuf buf= (ByteBuf) msg;
                            String s = ByteBufUtil.hexDump(buf);
                            System.out.println("确认包："+s);
                        }
                    });
                }
            });
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8889).sync();
            Channel channel = channelFuture.channel();
    }


    public static byte[] crc16CCITTFalse(byte[] bytes) {
        int crc = 0xffff; // initial value
        int polynomial = 0x1021; // poly value
        for (int index = 0; index < bytes.length; index++) {
            byte b = bytes[index];
            for (int i = 0; i < 8; i++) {
                boolean bit = ((b >> (7 - i) & 1) == 1);
                boolean c15 = ((crc >> 15 & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit){
                    crc ^= polynomial;
                }
            }
        }
        crc &= 0xffff;
        return new byte[] { (byte) ((0xff00 & crc) >> 8),(byte) (0xff & crc)  };
    }
}
